import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter/cupertino.dart';
import 'package:http_manager/http_manager.dart';
import '../../../utils/common/app_manager.dart';
export '../../../utils/networking/http_manager.dart';

class UserRequest {
  
  /// 获取 userToken
  static Future<ResponseAnalyzed> getUserToken(var uuid) async {
    String url = "/api/user/userToken";
    final param = {"uuid": uuid};
    debugPrint("登录不需要 userToken ，已设置测试代码的 userToken");
    // return await HttpManager.requestAnalyzing(HttpMethod.post, url, params: param);
    await Future.delayed(const Duration(seconds: 1));
    return ResponseAnalyzed(
        code: "200",
        data: "登录不需要 userToken ，已设置测试代码的 userToken",
        message: "登录不需要 userToken ，已设置测试代码的 userToken"
    );
  }

  /// 获取登录验证码
  static Future<ResponseAnalyzed> getCaptchaImage(var uuid) async {
    String url = "/api/captcha/image";
    return await HttpManager.getAnalyzing(url);
  }

  /// 发送验证码
  // static Future<ResponseAnalyzed> shortMessageCode(var value) async {
  //   String url = "";
  //   final param = {"": ""};
  //   return await HttpManager.requestAnalyzing(HttpMethod.post, url, params: param);
  // }

  /// 注册
  static Future<ResponseAnalyzed> registerAccount(var account, var password) async {
    String url = "/api/auth/register";
    final param = {"userName": account, "pwd": password, "role": "0"};
    return await HttpManager.postAnalyzing(url, params: param);
  }

  /// 密码登录
  // authType: "ACCOUNT"
  // captcha: "RMJQ"
  // clientId: "ef51c9a3e9046c4f2ea45142c8a8344a"
  // password: "Q06oawtyeI7ECdCnRX5rAHvQYQso6sPQm9cvZlqWNdqPilgpFt39/KjrcLtgPAgL8cdoXrfypEz45Ge7uaQUJQ=="
  // username: "admin"
  // uuid: "3fbe282b-3de1-4f87-8c99-631ffc6ee747"
  static Future<ResponseAnalyzed> loginWithPassword({
    required String account, 
    required String password, 
    String? code, 
    String? uuid
  }) async {
    String url = "/api/auth/login";
    final param = {
      "uuid": uuid ?? AppManager.uuid, 
      "authType": "ACCOUNT", 
      "clientId": "ef51c9a3e9046c4f2ea45142c8a8344b",
      "username": account, 
      "password": password
    };
    
    // 如果有验证码，则添加到请求参数中
    if (code != null && code.isNotEmpty) {
      param["captcha"] = code;
    }
    
    return await HttpManager.postAnalyzing(url, params: param);
  }



  /// 获取用户信息
  static Future<ResponseAnalyzed> getUserInfo() async {
    String url = "/api/auth/user/info";
    return await HttpManager.getAnalyzing(url);
  }

  /// 退出登录
  static Future<ResponseAnalyzed> logout({String account = "", String password = ''}) async {
    String url = "/api/auth/logout";
    final param = {"userName": account, "password": password};
    return await HttpManager.postAnalyzing(url, params: param);
  }


/// 短信登录
  // static Future<ResponseAnalyzed> loginWithShortMessage(var value) async {
  //   String url = "";
  //   final param = {"": ""};
  //   return await HttpManager.requestAnalyzing(HttpMethod.post, url, params: param);
  // }



  // 密码登录
  // static Future<ResponseAnalyzed> loginWithPassword({String account = "", String password = ''}) async {
  //   var account1 = account.replaceAll(" ", ""); // 如果有中文空格自动去除
  //   var account2 = account1.replaceAll(" ", ""); // 如果是英文空格自动去除
  //   String url = "/api/user/register";
  //
  //   final wdm5 = md5.convert(utf8.encode(password));
  //   final wdm5base64 = base64Encode(utf8.encode(wdm5.toString()));
  //   var deviceToken = await AppManager.getDeviceToken();
  //   final data = {
  //     "inputBox": account2,
  //     "password": wdm5base64,
  //     //"uuid": AppManager.uuid,// 不可以使用，否则在调用首页接口时会返回 20401 退出登录。
  //     "deviceToken": deviceToken ?? await AppManager.getDeviceToken(),
  //   };
  //   return await HttpManager.requestAnalyzing(HttpMethod.post, url, params: data);
  // }


  /// 忘记密码
  // static Future<ResponseAnalyzed> forgetPassword(var value) async {
  //   String url = "";
  //   final param = {"": ""};
  //   return await HttpManager.requestAnalyzing(HttpMethod.post, url, params: param);
  // }



  /// 上报用户同意隐私协议
  // static Future<ResponseAnalyzed> uploadingSigned(var uuid) async {
  //   String url = "";
  //   final param = {"uuid": ""};
  //   return await HttpManager.requestAnalyzing(HttpMethod.post, url, params: param);
  // }


}